<template>
  <div>
    <el-row style="margin-bottom:20px;display:flex;justify-content:flex-start">
      <el-select
        v-model="searchData.ttype"
        placeholder="请选择培训形式"
        style="margin-right:10px"
        clearable
      >
        <el-option
          v-for="item in ttypeArr"
          :key="item.value"
          :label="item.hash.code_name"
          :value="item.hash.code_id"
        ></el-option>
      </el-select>
      <el-select
        v-model="searchData.has_cert"
        placeholder="请选择是否取证"
        style="margin-right:10px"
        clearable
      >
        <el-option
          v-for="item in ishashArr"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select
        v-model="searchData.isbegin"
        placeholder="请选择计划状态"
        style="margin-right:10px"
        clearable
      >
        <el-option
          v-for="item in isbeginArr"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-input placeholder="培训内容" style="width:15%" v-model="searchData.tcontent" clearable></el-input>
      <el-date-picker
        v-model="pre_beginyear"
        style="margin:0 10px;"
        type="year"
        valueFormat="yyyy"
        placeholder="选择计划年度"
      ></el-date-picker>

      <!-- <el-date-picker
        clearable
        v-model="plandate"
        type="daterange"
        style="margin:0 10px;"
        rangeSeparator="至"
        startPlaceholder="培训开始日期"
        endPlaceholder="培训开始日期"
        valueFormat="yyyy-MM-dd"
      ></el-date-picker>
      <el-date-picker
        clearable
        v-model="plandate2"
        type="daterange"
        style="margin:0 10px;"
        rangeSeparator="至"
        startPlaceholder="实际开始日期"
        endPlaceholder="实际开始日期"
        valueFormat="yyyy-MM-dd"
      ></el-date-picker>-->
      <el-button size="mini" type="primary" @click="getplanlist">搜索</el-button>
    </el-row>

    <el-row>
      <el-button size="mini" @click="showplanadd">添加培训计划</el-button>
      <el-button size="mini" type="primary" @click="editplan">编辑</el-button>
      <el-button size="mini" type="success" @click="deleteperson">删除</el-button>
      <el-button size="mini" type="info" @click="daochu">导出</el-button>
      <span style="color:#f60;margin-left:10px">(已开始状态下，无法编辑和删除)</span>

      <!-- <el-button size="mini" type="info" style="float:right" @click="usertype=!usertype">切换角色</el-button> -->
    </el-row>
    <el-divider></el-divider>
    <plantable :plantable="plantable" :choosedp.sync="choosedp" :pagobj.sync="pagobj"></plantable>
    <plantable
      :plantable="newplantable"
      :choosedp.sync="choosedp"
      v-show="false"
      :pagobj.sync="pagobj"
      id="tableExcelsssssss"
    ></plantable>
    <addplan v-if="showaddplan" :editdata="editdata" :isedit="isedit"></addplan>
  </div>
</template>

<script>
import addplan from '~/components/person/workplan/addplan.vue';
import plantable from '~/components/person/workplan/plantable.vue';
import FileSaver from 'file-saver';
import XLSX from 'xlsx';

export default {
  data() {
    return {
      ts: this.newStore(),
      choosedp: [],
      pagobj: {
        currentPage3: 1,
        pagesize: 10,
        total: 10,
      },
      isedit: false,
      newplantable: [],
      searchData: {
        ttype: '',
        has_cert: '',
        tcontent: '',
        isbegin: '',
        pre_begintime_begin: '',
        pre_begintime_end: '',
        starttime_begin: '',
        starttime_end: '',
      },
      usertype: true,
      showaddplan: false,
      plantable: [],
      ttypeArr: [],
      classifyArr: [],
      editdata: {},
      plandate: ['', ''],
      plandate2: ['', ''],
      pre_beginyear: '',
      ishashArr: [
        {
          label: '是',
          value: '1',
        },
        {
          label: '否',
          value: '0',
        },
      ],
      isbeginArr: [
        {
          label: '未开始',
          value: '0',
        },
        {
          label: '已开始',
          value: '1',
        },
      ],
      planyear: '',
    };
  },
  created() {
    this.getcodeData('0206', '02', 'ttypeArr');
    this.getcodeData('0207', '02', 'classifyArr');

    this.pre_beginyear = `${new Date().getFullYear()}`;
    this.getplanlist();
  },
  methods: {
    // 删除培训记录
    deleteperson() {
      if (this.choosedp.length == 0) {
        this.$message({
          type: 'info',
          message: '请先勾选记录',
        });
        return;
      }
      const deletearr = [];
      for (let i = 0; i < this.choosedp.length; i += 1) {
        deletearr.push(this.choosedp[i].hash.id);
      }

      this.$confirm('确认删除已选择的培训记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const req = {
            handle:
              '33224548EFC2E94F19649E9176434E4493065B35648D1B6FB0860307829EB17A',
            id: deletearr,
          };
          this.spost(this.ts, '/mainservlet', req).then(() => {
            this.$message({
              type: 'success',
              message: '删除成功',
            });
            this.getplanlist();
          });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消',
          });
        });
    },
    // 编辑培训记录
    editplan() {
      if (this.choosedp.length == 0) {
        this.$message({
          type: 'info',
          message: '请先培训记录',
        });
        return;
      }
      if (this.choosedp.length > 1) {
        this.$message({
          type: 'info',
          message: '只能编辑一条记录',
        });
        return;
      }
      const req = {
        handle: '33224548EFC2E94F97536D55316F44F2C07046ED2C6C984F',
        id: this.choosedp[0].hash.id,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.isedit = true;
        this.showaddplan = true;
        this.editdata = ts.d.data;
        this.reload('showaddplan');
      });
    },
    // 公共方法获取代码项
    getcodeData(supid, codetype, arrname) {
      const req = {
        handle: '67A5B6FEF17D97DECF116BDF95B95C0B167942DD3F1604F9',
        supid,
        codetype,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this[arrname] = ts.d.data.operation1;
      });
    },
    // 公共组件刷新方法
    reload(partsname) {
      this[partsname] = false;
      this.$nextTick(() => {
        this[partsname] = true;
      });
    },
    // 点击添加培训记录
    showplanadd() {
      this.isedit = false;
      this.showaddplan = false;
      this.reload('showaddplan');
    },
    // 获取培训计划列表
    getplanlist() {
      // if (this.plandate == null) {
      //   this.plandate = [];
      // }
      // if (this.plandate2 == null) {
      //   this.plandate2 = [];
      // }
      const req = this.searchData;
      req.pre_beginyear = this.pre_beginyear;
      req.start = (this.pagobj.currentPage3 - 1) * this.pagobj.pagesize;
      req.limit = this.pagobj.pagesize;
      req.handle =
        '33224548EFC2E94FE9F3B6004AA9373090D3E1223364DCF2BFF0B9210751F692';
      [req.pre_begintime_begin] = [this.plandate[0]];
      [req.pre_begintime_end] = [this.plandate[1]];
      [req.starttime_begin] = [this.plandate2[0]];
      [req.starttime_end] = [this.plandate2[1]];
      console.log(req);

      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.plantable = ts.d.data.operation1;
        this.pagobj.total = parseInt(ts.d.data.operation2[0].hash.allrs);
      });
      this.getplanlistasssssss();
    },
    // 获取培训计划列表
    getplanlistasssssss() {
      // if (this.plandate == null) {
      //   this.plandate = [];
      // }
      // if (this.plandate2 == null) {
      //   this.plandate2 = [];
      // }
      const req = this.searchData;
      req.pre_beginyear = this.pre_beginyear;
      req.start = (this.pagobj.currentPage3 - 1) * this.pagobj.pagesize;
      req.limit = 1000000000;
      req.handle =
        '33224548EFC2E94FE9F3B6004AA9373090D3E1223364DCF2BFF0B9210751F692';
      [req.pre_begintime_begin] = [this.plandate[0]];
      [req.pre_begintime_end] = [this.plandate[1]];
      [req.starttime_begin] = [this.plandate2[0]];
      [req.starttime_end] = [this.plandate2[1]];
      console.log(req);

      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.newplantable = ts.d.data.operation1;
        this.pagobj.total = parseInt(ts.d.data.operation2[0].hash.allrs);
      });
    },
    // 导出功能
    daochu() {
      if (this.plantable.length == 0) {
        this.$message({
          type: 'error',
          message: '导出数据为空,无法导出',
        });
        return false;
      }
      const xlsxParam = { raw: true }; // 转换成excel时，使用原始的格式
      const wb = XLSX.utils.table_to_book(
        document.querySelector('#tableExcelsssssss'),
        xlsxParam,
      );
      const wbout = XLSX.write(wb, {
        bookType: 'xlsx',
        bookSST: true,
        type: 'array',
      });
      try {
        FileSaver.saveAs(
          new Blob([wbout], { type: 'application/octet-stream;charset=utf-8' }),
          'sheetjs.xlsx',
        );
      } catch (e) {
        if (typeof console !== 'undefined') console.log(e, wbout);
      }
      return wbout;
    },
  },
  components: {
    addplan,
    plantable,
  },
};
</script>

